package com.diorsunion.hedge.dal.mybatis;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Currency;

/**
 * Created by harley-dog on 2016/5/6.
 */
public class CurrencyEnumStatusHandler extends BaseTypeHandler<Currency> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Currency currency, JdbcType jdbcType) throws SQLException {
        // baseTypeHandler已经帮我们做了parameter的null判断
        preparedStatement.setString(i, currency.toString());
    }

    @Override
    public Currency getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        if (resultSet.wasNull()) {
            return null;
        } else {
            String str = resultSet.getString(columnName);
            // 根据数据库中的code值，定位EnumStatus子类
            return Currency.getInstance(str);
        }
    }

    @Override
    public Currency getNullableResult(ResultSet resultSet, int i) throws SQLException {
        if (resultSet.wasNull()) {
            return null;
        } else {
            String str = resultSet.getString(i);
            // 根据数据库中的code值，定位EnumStatus子类
            return Currency.getInstance(str);
        }
    }

    @Override
    public Currency getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        if (callableStatement.wasNull()) {
            return null;
        } else {
            String str = callableStatement.getString(i);
            // 根据数据库中的code值，定位EnumStatus子类
            return Currency.getInstance(str);
        }
    }
}
